using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using DG.Tweening;

public class SlideScrollView : MonoBehaviour, IBeginDragHandler, IEndDragHandler
{
    private RectTransform contentTrans;
    private float beginMousePosX;
    private float endMousePosX;
    private ScrollRect scrollRect;

    public int cellLength;//单元格长度
    public int spacing;//单元格间距
    public int leftOffset;
    private float moveOneItemLength;//移动一个单元格的距离

    private Vector3 currentContentLocalPos;
    private Vector3 contentInitPos;//content初始位置
    private Vector2 contentTransSize;//content初始大小

    public int totalItemNum;//总单元格数量
    private int currentIndex;//当前单元格索引
    public Text pageText;

    public bool needSendMessage;//是否发送消息
    public void Awake()
    {
        scrollRect = GetComponent<ScrollRect>();
        contentTrans = scrollRect.content;
        moveOneItemLength = cellLength + spacing;
        currentContentLocalPos = contentTrans.localPosition;
        contentInitPos = contentTrans.localPosition;
        contentTransSize = contentTrans.sizeDelta;
        currentIndex = 1;
        if (pageText != null)
        {
            pageText.text = currentIndex.ToString() + "/" + totalItemNum;
        }
    }

    public void Init()
    {
        // Debug.Log(contentInitPos);
        // Debug.Log(contentTrans);
        currentIndex = 1;
        if (contentTrans != null)
        {
            contentTrans.localPosition = contentInitPos;
            currentContentLocalPos = contentInitPos;
            if (pageText != null)
            {
                pageText.text = currentIndex.ToString() + "/" + totalItemNum;
            }
        }

    }

    /// <summary>
    /// 拖拽翻书效果
    /// </summary>
    /// <param name="eventData"></param>
    public void OnEndDrag(PointerEventData eventData)
    {
        endMousePosX = Input.mousePosition.x;
        float offSetX = 0;
        float moveDistance = 0;//当次需要滑动的距离
        offSetX = beginMousePosX - endMousePosX;

        if (offSetX > 0)//右滑
        {
            if (currentIndex >= totalItemNum)
            {
                return;
            }
            if (needSendMessage)
            {
                UpdatePanel(true);
            }
            moveDistance = -moveOneItemLength;
            currentIndex++;
        }
        else//左滑
        {
            if (currentIndex <= 1)
            {
                return;
            }
            if (needSendMessage)
            {
                UpdatePanel(false);
            }

            moveDistance = moveOneItemLength;
            currentIndex--;
        }
        if (pageText != null)
        {
            pageText.text = currentIndex.ToString() + "/" + totalItemNum;
        }
        DOTween.To(() => contentTrans.localPosition, lerpValue => contentTrans.localPosition = lerpValue, currentContentLocalPos + new Vector3(moveDistance, 0, 0), 0.5f).SetEase(Ease.OutQuint);
        currentContentLocalPos += new Vector3(moveDistance, 0, 0);
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        beginMousePosX = Input.mousePosition.x;
    }

    /// <summary>
    /// 按钮翻书效果
    /// </summary>
    public void ToNextPage()
    {
        float moveDistance = 0;
        if (currentIndex >= totalItemNum)
        {
            return;
        }
        moveDistance = -moveOneItemLength;
        currentIndex++;
        if (pageText != null)
        {
            pageText.text = currentIndex.ToString() + "/" + totalItemNum;
        }
        if (needSendMessage)
        {
            UpdatePanel(true);
        }
        DOTween.To(()
          => contentTrans.localPosition, lerpValue
          => contentTrans.localPosition = lerpValue,
          currentContentLocalPos + new Vector3(moveDistance, 0, 0), 0.5f)
          .SetEase(Ease.OutQuint);

        currentContentLocalPos += new Vector3(moveDistance, 0, 0);
    }
    public void ToLastPage()
    {
        float moveDistance = 0;
        if (currentIndex <= 1)
        {
            return;
        }
        moveDistance = moveOneItemLength;
        currentIndex--;
        if (pageText != null)
        {
            pageText.text = currentIndex.ToString() + "/" + totalItemNum;
        }
        if (needSendMessage)
        {
            UpdatePanel(false);
        }

        DOTween.To(()
          => contentTrans.localPosition, lerpValue
          => contentTrans.localPosition = lerpValue,
          currentContentLocalPos + new Vector3(moveDistance, 0, 0), 0.5f)
          .SetEase(Ease.OutQuint);

        currentContentLocalPos += new Vector3(moveDistance, 0, 0);
    }

    /// <summary>
    /// 设置Content的大小
    /// </summary>
    /// <param name="itemNum"></param>
    public void SetContentLength(int itemNum)
    {
        contentTrans.sizeDelta = new Vector2(contentTrans.sizeDelta.x + (cellLength + spacing) * (itemNum - 1), contentTrans.sizeDelta.y);
        totalItemNum = itemNum;
    }

    //初始化Content的大小
    public void InitScrollLength()
    {
        contentTrans.sizeDelta = contentTransSize;
    }

    //发送翻页信息的方法
    public void UpdatePanel(bool toNext)
    {
        if (toNext)
        {
            gameObject.SendMessageUpwards("ToNextLevel");
        }
        else
        {
            gameObject.SendMessageUpwards("ToLastLevel");
        }
    }
}
